package com.digitalart.commons
{
	import flash.display.Loader;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.net.URLRequest;    
	
	/**
	 * @author ulises.ocana 
	 */
	public class CustomAssetLoader extends EventDispatcher {
		
		private static var _instance : CustomAssetLoader;
		
		public static const ON_BULK_LOADER_COMPLETE : String = "onBulkLoaderComplete";
		
		public static const ON_ASSET_LOAD_COMPLETE : String = "on_asset_load_complete";
		
		private var _bulkLoaderlist : Array;
		
		private var _bulkLoaderPathList : Array;
		
		private var _counter : int;
		
		/**********************************Constructor*************************/
		
		public function CustomAssetLoader(singleton : SingletonEnforcer ) {
			_counter = 0;
			_bulkLoaderlist = new Array();
		}
		
		
		/**********************************************************************/
		
		/**********************************Methods*************************/
		
		public function loadAsset(path : String):void{
			var loader:Loader = new Loader();
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, completeLoading);
			loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,failLoadingAsset);
			loader.load(new URLRequest(path));      
		}
		
		private function completeLoading(e:Event):void {
			trace("[ASSET LOADED]");
			e.target.removeEventListener(Event.COMPLETE, completeLoading);                                  
			dispatchEvent(new CustomEvent(ON_ASSET_LOAD_COMPLETE,e.target.content));
		}
		
		
		
		public function bulkLoad() : void {             
			
			if(_counter >= _bulkLoaderPathList.length){                                                             
				_counter = 0;
				dispatchEvent(new CustomEvent(ON_BULK_LOADER_COMPLETE, _bulkLoaderlist));                               
				_bulkLoaderlist = new Array();
			}else{                          
				var loader:Loader = new Loader();
				loader.contentLoaderInfo.addEventListener(Event.COMPLETE, completePartialbulkLoading);
				loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,failLoadingAsset);                                                                      
				loader.load(new URLRequest(_bulkLoaderPathList[_counter]));     
			}
		}
		
		private function failLoadingAsset(event : IOErrorEvent) : void {
			trace("Fail loading asset " + event.text);
		}
		
		private function completePartialbulkLoading(e : Event) : void {                 
			_bulkLoaderlist.push(e.target.content);
			_counter ++;
			bulkLoad();
		}       
		
		/**********************************************************************/
		/**************************Getters & Setters***************************/
		
		
		public static function getInstance(): CustomAssetLoader{
			if(_instance == null){
				_instance = new CustomAssetLoader(new SingletonEnforcer());
			}
			return _instance;
		}
		
		public function get bulkLoaderPathList() : Array {
			return _bulkLoaderPathList;
		}
		
		public function set bulkLoaderPathList(bulkLoaderPathList : Array) : void {
			_bulkLoaderPathList = bulkLoaderPathList;
		}
	}
}
class SingletonEnforcer{}